package com.penghe.www.httplibrary

import com.google.gson.Gson
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.*

/**
 * Created by pengh on 2017/5/29.
 * 网路请求的回调
 */

 abstract class HttpCallBack<Result> :ICallBack{
    override fun onSucceeded(result: String) {
        val gson: Gson = Gson()
        val clazz:Class<*> = analysisClassInfo(this)
        @Suppress("UNCHECKED_CAST")
        val resultObject: Result = gson.fromJson(result,clazz) as Result
        onSucceeded(resultObject)
    }

    abstract fun onSucceeded(result: Result)

    private fun analysisClassInfo(any: Any):Class<*>{
        val genType: Type = any.javaClass.genericSuperclass
        val params:Array<Type> = (genType as ParameterizedType).actualTypeArguments
        return params[0] as Class<*>
    }
}